package com.wu.leetcode.leetcode46;

import java.util.ArrayList;
import java.util.List;

/**
 * Leetcode46-全排列
 *
 * @author lynn
 * @date 2020/8/14 - 16:51
 */
class Solution {
    static List<List<Integer>> res = new ArrayList<>();

    public static List<List<Integer>> permute(int[] nums) {
        if (nums == null || nums.length == 0) {
            return res;
        }
        boolean[] vistied = new boolean[nums.length];

        dfs(nums, 0, nums.length, new ArrayList<Integer>(), vistied);


        return res;
    }

    private static void dfs(int[] nums, int i, int length, ArrayList<Integer> list, boolean[] vistied) {
        if (i == length) {
            res.add(new ArrayList<>(list));
            return;
        }
        for (int j = 0; j < length; j++) {
            if (!vistied[j]) {
                list.add(nums[j]);
                vistied[j] = true;
                dfs(nums, i + 1, length, list, vistied);
                vistied[j] = false;
                list.remove(list.size() - 1);
            }
        }
    }

    public static void main(String[] args) {
        int[] arr = new int[]{1,2,3};
        List<List<Integer>> permute = permute(arr);
        System.out.println(permute);
    }
}
